import pandas as pd
import json

# 文件共13000行，cifar10：1-3000；cifar100:3001-6000；imagenetD：6001-8000；food101:8001-11000；imagenet32:11001-13000
# 定义函数来逐行读取并解析JSONL文件
def read_jsonl(file_path):
    data = []
    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            data.append(json.loads(line.strip()))
    return data

# 提取前600行的"text"和"label":cifar10,cifar100,food101.提取imagenet32,imagenetD前400行
file_path = "15Bcanyoufind\\15Bcanyoufind.jsonl"  # 替换为你的jsonl文件路径
data1 = read_jsonl(file_path)[:600]  # 取前600行
data2 = read_jsonl(file_path)[3001:3600]  
data3 = read_jsonl(file_path)[6001:6400]
data4 = read_jsonl(file_path)[8001:8600]
data5 = read_jsonl(file_path)[11001:11400]
extracted_data1 = [{"text": item.get("text"), "label": item.get("label")} for item in data1 if "text" in item and "label" in item]
extracted_data2 = [{"text": item.get("text"), "label": item.get("label")} for item in data2 if "text" in item and "label" in item]
extracted_data3 = [{"text": item.get("text"), "label": item.get("label")} for item in data3 if "text" in item and "label" in item]
extracted_data4 = [{"text": item.get("text"), "label": item.get("label")} for item in data4 if "text" in item and "label" in item]
extracted_data5 = [{"text": item.get("text"), "label": item.get("label")} for item in data5 if "text" in item and "label" in item]

# 创建DataFrame
df1 = pd.DataFrame(extracted_data1)
df2 = pd.DataFrame(extracted_data2)
df3 = pd.DataFrame(extracted_data3)
df4 = pd.DataFrame(extracted_data4)
df5 = pd.DataFrame(extracted_data5)

# 写入Excel文件
output_file_1 = '15Bcanyoufind\output—_cifar10.xlsx'  # 输出Excel文件名
output_file_2 = '15Bcanyoufind\output—_cifar100.xlsx'
output_file_3 = '15Bcanyoufind\output—_imagenetD.xlsx'
output_file_4 = '15Bcanyoufind\output—_food101.xlsx'
output_file_5 = '15Bcanyoufind\output—_imagenet32.xlsx'
df1.to_excel(output_file_1, index=False)
df2.to_excel(output_file_2, index=False)
df3.to_excel(output_file_3, index=False)
df4.to_excel(output_file_4, index=False)
df5.to_excel(output_file_5, index=False)
print(f"数据已成功写入")